package com.webwork.lims.transport.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.transport.dataobject.TransType;
import com.webwork.lims.transport.dataobject.Ways;
import com.webwork.lims.transport.service.WaysService;
import com.webwork.lims.transport.service.ZoneService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/transport")
public class WaysController extends SuperCtrl {

	@Autowired
	private WaysService waySve;
	
	@Autowired
	private ZoneService zoneSve;
	
	@RequestMapping(value = "/ways")
	@Override
	public String mainPage() {
		return "ways";
	}
	
	@RequestMapping(value = "queryWaysList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Ways queryObj = new Ways();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Ways.class);
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", waySve.count(queryObj));
		ret.put("rows", waySve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getWay")
	@ResponseBody
	public ResponseEntity<String> getWay(@RequestParam("wayCode") int wayCode) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Ways way = waySve.get(wayCode);
		return new ResponseEntity<String>(new Gson().toJson(way).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "getWayLine")
	@ResponseBody
	public ResponseEntity<String> getWayLine(@RequestParam("wayLine") String wayLine) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		String ret = "";
		ret = zoneSve.toChinese(wayLine);
		return new ResponseEntity<String>(ret, responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "addWay")
	@ResponseBody
	public String addWay(@RequestParam("jsonObj") String jsonObj, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Ways way = new Gson().fromJson(jsonObj, Ways.class);
		if (waySve.get(way.getWayCode()) != null) {
			return EXIST;
		}
		waySve.save(way);
		return SUCCESS;
	}
	
	@RequestMapping(value = "editWay")
	@ResponseBody
	public String editWay(@RequestParam("jsonObj") String jsonObj) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Ways z = new Gson().fromJson(jsonObj, Ways.class);
		Ways way = waySve.get(z.getWayCode());
		if (way == null) {
			return NOT_EXIST;
		}
		way.setStartCode(z.getStartCode());
		way.setEndCode(z.getEndCode());
		way.setType(z.getType());
		way.setHour(z.getHour());
		waySve.save(way);
		return SUCCESS;
	}
	
	@RequestMapping(value = "delWay")
	@ResponseBody
	public String delWay(@RequestParam("id") int id, HttpServletRequest request) {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Ways way = waySve.get(id);
		if (way == null) {
			return FAIL;
		}
		waySve.delete(id);
		
		return SUCCESS;
	}
	
	@RequestMapping(value="loadWayType")
	@ResponseBody
	public ResponseEntity<String> loadWayType() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		TransType[] types = TransType.values();
		List<Object> ret = new ArrayList<Object>();
		for (TransType o : types) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.name());
			map.put("name", o.name());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
